package team.hyznrj.studentsys.utils;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;

import team.hyznrj.studentsys.entity.MongoArregateData;
/**
 * mongoREST集成工具
 * @ClassName HighMongoUtil
 * @author 郑泽锋
 * @date 2018年4月11日
 * @param <T>
 */
@Component
public class HighMongoUtil<T> {

	@Autowired
	private MongoTemplate mongoTemplate;
	public MongoTemplate getTemplate(){
		return this.mongoTemplate;
	}
	/**
	 * 将data保存在集合collectionName中
	 * @param data
	 * @param collectionName
	 * @return void
	 */
	public void save(T data,String collectionName) {
		mongoTemplate.save(data,collectionName);
	}
	/**
	 * 将多个data批量插入到集合collectionName中
	 * @param data
	 * @param collectionName
	 * @return void
	 */
	public void saveList(List<T> data,String collectionName) {
		mongoTemplate.insert(data, collectionName);
	}
	/**
	 * 单记录更新
	 * @param query
	 * @param update
	 * @param collectionName
	 * @return
	 * @return WriteResult
	 */
	public WriteResult updateFirst(Query query,Update update,String collectionName) {
		WriteResult updateFirst = mongoTemplate.updateFirst(query, update, collectionName);
		return updateFirst;
	}
	/**
	 * 多记录更新
	 * @param query
	 * @param update
	 * @param collectionName
	 * @return
	 * @return WriteResult
	 */
	public WriteResult updateMulity(Query query,Update update,String collectionName) {
		WriteResult updateFirst = mongoTemplate.updateMulti(query, update, collectionName);
		return updateFirst;
	}
	/**
	 * 通过条件删除信息
	 * delete  
	 * @param data
	 * @param collectionName
	 * @return
	 * @return WriteResult
	 */
	public WriteResult delete(Query query,String collectionName) {
		WriteResult remove = mongoTemplate.remove(query, collectionName);
		return remove;
	}
	/**
	 * 通过id删除信息（单一）
	 * deleteById  
	 * @param query
	 * @param collectionName
	 * @return
	 * @return Object
	 */
	public Object deleteById(Query query,String collectionName){
		Object remove = mongoTemplate.findAndRemove(query, BasicDBObject.class, collectionName);
		return remove;
	}
	/**
	 * 查询单条数据
	 * find  
	 * @param query
	 * @param entityClass
	 * @return
	 * @return Object
	 */
	public T find(Query query,Class<T> entityClass,String collectionName){
		return mongoTemplate.findOne(query, entityClass,collectionName);
	}
	/**
	 * 根据条件查询多条数据
	 * findMulity  
	 * @param query
	 * @param entityClass
	 * @return
	 * @return List<T>
	 */
	public List<T> findMulity(Query query,Class<T> entityClass,String collectionName){
		return mongoTemplate.find(query, entityClass, collectionName);
	}
	/***
	 * 获取聚合数据
	 * @param keyWords
	 * @param query
	 * @param entityClass
	 * @param collectionName
	 * @return
	 */
	public MongoArregateData group(String keyWords,Query query,Class<T> entityClass,String collectionName){
		String reduce="function Reduce(doc, out) {"
				+ "out.count+=1;}";
		DBObject queryObject=null;
		if(query!=null){
			queryObject= query.getQueryObject();
		}
		BasicDBList group = (BasicDBList)mongoTemplate.getCollection(collectionName).group(new BasicDBObject(keyWords, 1), queryObject,
				new BasicDBObject("count",0), reduce);
		if(group!=null&&group.size()>0){
			MongoArregateData data=new MongoArregateData();
			for(int i=0;i<group.size();i++){
				BasicDBObject object = (BasicDBObject)group.get(i);
				data.save(object.getString(keyWords), object.getInt("count"));
			}
			return data;
		}
		return null;
	}
	
	public long count(Query query,Class<T> entityClass,String collectionName){
		return mongoTemplate.count(query, entityClass, collectionName);
	}
}
 